package com.ec.vs.encrypt;

import cn.hutool.crypto.symmetric.AES;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.core.env.Environment;

public final class AesEncryptor {

    private static final AES AES;

    static {
        Environment env = ApplicationContextProvider.getEnvironment();
        String key = env.getProperty("aes.key", "0123456789abcdef"); // 兜底
        AES = new AES(key.getBytes());
    }

    public static String encrypt(String plain) {
        return plain == null ? null : HexUtil.encodeHexStr(AES.encrypt(plain));
    }

    public static String decrypt(String cipher) {
        return cipher == null ? null : StrUtil.utf8Str(AES.decrypt(HexUtil.decodeHex(cipher)));
    }
}